/*  KKS - KinoKassenSystem 2013
 * 
 *  @author Sebastian Schöppner
 * 
 *  numpad for various inputs
 * 
 *  
 */
package kks;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.ListModel;

public class PanelNumpad extends JPanel 
{
    //numpad
    private JButton btnNumpad0point0;
    private JButton btnNumpad0;
    private JButton btnNumpad1;
    private JButton btnNumpad2;
    private JButton btnNumpad3;
    private JButton btnNumpad4;
    private JButton btnNumpad5;
    private JButton btnNumpad6;
    private JButton btnNumpad7;
    private JButton btnNumpad8;
    private JButton btnNumpad9;
    private JButton btnNumpadDel;
    
    private double dSum;
    private double dCurrent;
    private cMovie Movie;
    
    //ticket buttons
    private JButton btnNumpadAnnul;
    private JButton btnNumpadGet;
    private JButton btnNumpadReserve;
    
    
    private JButton btnNumpadAdd;
     private JButton btnNumpadCash;
    
    private ActionListener listenerbtnNumpadNumbers;
    private ActionListener listenerbtnNumpadActions;
    
    
    private JFormattedTextField txtNumpadSum;     
    private JList lstItems;
    private DefaultListModel listModel;
    private JTextField txtNumpadCurrentItem;
    private JFormattedTextField txtNumpadCurrentPrice;
    private JFormattedTextField txtNumpadCurrent;
    private JTextField txtNumpadCash;
    private JButton btnNumpadCFinal;
    private cBooking Booking;
    private String CashString ="";
    /**
     *
     */
    public PanelNumpad()
    {
        
        //setLayout(new GridBagLayout());        
        setLayout(new GridLayout(4,0));
        initComps();
     
    }
    
    /**
    * initialize all components 
    * <p>
    *
    * 
    */    
    private boolean initComps()
    {
       
       try
       {           
            //create action listeners for numberbuttons
            listenerbtnNumpadNumbers = new ActionListener() 
            {

                @Override
                public void actionPerformed(ActionEvent e) 
                {                                      
                    CashString += e.getActionCommand();
                    txtNumpadCurrentPrice.setValue(Double.parseDouble(CashString));                  
                 };
            };
            
            //create action listeners for actionButtons
            listenerbtnNumpadActions = new ActionListener() 
            {
                
                @Override
                public void actionPerformed(ActionEvent e) 
                {                    
                    int item = listModel.getSize()-1;
                    
                    switch(e.getActionCommand())
                    {
                        case "delete":
                        {                
                                      
                            subsFromSum(dCurrent);
                            listModel.remove(listModel.getSize()-1);                          
                            listModel.remove(listModel.getSize()-1);
                            break;
                        }
                            
                        case "add":
                        {
                            listModel.addElement(txtNumpadCurrentItem.getText());
                            listModel.addElement(txtNumpadCurrent.getText());                            
                            addToSum();
                            break;
                        }
                            
                        case "final":
                        {
                            txtNumpadCurrent.setValue(getCashBack());
                            TicketFrame ticketFrame = new TicketFrame(Booking);
                            ticketFrame.setTitle("Ticket");
                            break;
                        }
                                                        
                            
                        case "cash" :
                        {
                            txtNumpadCurrentPrice.setValue(0);
                            txtNumpadCurrent.setValue(0);
                            CashString = "";
                            txtNumpadCurrentItem.setText("CASH");                            
                            break;
                        }
                    }
                };
            };    
            
            btnNumpad7 = new JButton("7");           
            btnNumpad8 = new JButton("8");
            btnNumpad9 = new JButton("9");
            btnNumpad4 = new JButton("4");
            btnNumpad5 = new JButton("5");
            btnNumpad6 = new JButton("6");
            btnNumpad1 = new JButton("1");
            btnNumpad2 = new JButton("2");
            btnNumpad3 = new JButton("3");         
            btnNumpad0point0 = new JButton(".00");
            btnNumpad0= new JButton("0");            
                 
            btnNumpadAdd = new JButton("add");
            btnNumpadDel = new JButton("delete");
            btnNumpadCFinal = new JButton("final");
            btnNumpadCFinal.setBackground(CustomColors.MainColor);            
            btnNumpadCash = new JButton("cash");
            btnNumpadCash.setBackground(CustomColors.MainColorLight);
            btnNumpadAnnul = new JButton("A");
            
            btnNumpadGet = new JButton("G");            
            btnNumpadReserve = new JButton("R");
                    
            btnNumpad0point0.addActionListener(listenerbtnNumpadNumbers);            
            btnNumpad0.addActionListener(listenerbtnNumpadNumbers);                        
            btnNumpad1.addActionListener(listenerbtnNumpadNumbers);                                    
            btnNumpad2.addActionListener(listenerbtnNumpadNumbers);  
            btnNumpad3.addActionListener(listenerbtnNumpadNumbers);             
            btnNumpad4.addActionListener(listenerbtnNumpadNumbers);
            btnNumpad5.addActionListener(listenerbtnNumpadNumbers);
            btnNumpad6.addActionListener(listenerbtnNumpadNumbers);
            btnNumpad7.addActionListener(listenerbtnNumpadNumbers);
            btnNumpad8.addActionListener(listenerbtnNumpadNumbers);
            btnNumpad9.addActionListener(listenerbtnNumpadNumbers);
            
            btnNumpadDel.addActionListener(listenerbtnNumpadActions);            
            btnNumpadAdd.addActionListener(listenerbtnNumpadActions);
            btnNumpadCash.addActionListener(listenerbtnNumpadActions);
            btnNumpadCFinal.addActionListener(listenerbtnNumpadActions);

        
        // add components to numpad
      
            listModel = new DefaultListModel();            
                        
            
            JPanel pnlNumbers = new JPanel(new GridLayout(4, 0));
            JPanel bottomNumpad = new JPanel(new GridLayout(2, 0));            
            JPanel pnlCurrent = new JPanel(new GridLayout(3, 0));
            JPanel pnlTickets = new JPanel(new GridLayout(2, 0));
            JPanel pnlSum = new JPanel(new GridLayout(1, 0));
            
            NumberFormat paymentFormat = NumberFormat.getCurrencyInstance();
            
            pnlCurrent.add(txtNumpadCurrentItem = new JTextField());
                txtNumpadCurrentItem.setEditable(false);
                txtNumpadCurrentItem.setHorizontalAlignment(JTextField.RIGHT);
                txtNumpadCurrentItem.setFont(CustomFonts.FontSubline);
                
            pnlCurrent.add(txtNumpadCurrentPrice = new JFormattedTextField(paymentFormat));
                txtNumpadCurrentPrice.setEditable(false);
                txtNumpadCurrentPrice.setHorizontalAlignment(JTextField.RIGHT);
                txtNumpadCurrentPrice.setFont(CustomFonts.FontHeadline);
                txtNumpadCurrentPrice.setValue(new Double(0));
                        
            pnlCurrent.add(txtNumpadCurrent = new JFormattedTextField(paymentFormat));
                txtNumpadCurrent.setEditable(false);
                txtNumpadCurrent.setHorizontalAlignment(JTextField.RIGHT);
                txtNumpadCurrent.setFont(CustomFonts.FontHeadline);
                txtNumpadCurrent.setValue(new Double(0));
            

                                                                                               
            pnlNumbers.add(btnNumpad7);
            pnlNumbers.add(btnNumpad8);
            pnlNumbers.add(btnNumpad9);
            pnlNumbers.add(btnNumpad4);
            pnlNumbers.add(btnNumpad5);
            pnlNumbers.add(btnNumpad6);
            pnlNumbers.add(btnNumpad1);
            pnlNumbers.add(btnNumpad2);
            pnlNumbers.add(btnNumpad3);
            pnlNumbers.add(btnNumpad0point0);
            pnlNumbers.add(btnNumpad0);            
            
            
            txtNumpadSum = new JFormattedTextField(paymentFormat);
            txtNumpadSum.setEditable(false);
            txtNumpadSum.setHorizontalAlignment(JTextField.RIGHT);
            txtNumpadSum.setFont(CustomFonts.FontHeadline);           
            txtNumpadSum.setValue(new Double(0));
            pnlSum.add(txtNumpadSum);
            
                       
            pnlTickets.add(btnNumpadAdd);
            pnlTickets.add(btnNumpadDel);
            pnlTickets.add(btnNumpadCash);
            pnlTickets.add(btnNumpadCFinal);          

            
            
            add(lstItems = new JList(listModel));
            add(pnlCurrent);
            bottomNumpad.add(pnlTickets);         
            bottomNumpad.add(pnlSum);
            add(pnlNumbers);
            add(bottomNumpad);   
            
            
            return true;
       }
       catch(Exception e)
       {
           return false;
       }       
       
    }

    /**
     *  set the Sum and Sum label
     *  @param the sum to set
     */    
    public void setSum(double value)
    {
        dSum = value;
        //txtNumpadSum.setText(Double.toString(dSum));
        txtNumpadSum.setValue(dSum);
        
    }
    
    /**
     *  get the Sum
     *  @return the sum
     */       
    public double getSum()
    {
        return dSum;
        //return Integer.parseInt(txtNumpadSum.getText());
        
    }
    
    /**
     *  add to the sum and update the label
     *  @param the value to add
     */     
    public void addToSum(double value)
    {
        dSum += value;        
        updateSum();
    }
    
    /**
     *  add to the sum from the current label and update the sum label
     *  
     */      
    public void addToSum()
    {
        //dSum += Double.valueOf(txtNumpadCurrent.getText());        
        dSum += (Double)txtNumpadCurrent.getValue();
        updateSum();
    }
    
    /**
     *  substract from the sum and update the label
     *  @param the value to substract
     */       
    public void subsFromSum(double value)
    {
        if  ((dSum - value) > 0)
            dSum -= value;
        updateSum();
    }

     /**
     *  update the Sum label
     */
    public void updateSum()
    {     
        //txtNumpadSum.setText(Double.toString(dSum));
        txtNumpadSum.setValue(dSum);
    }
    
    
    public void setCurrent(double value)
    {
        dCurrent = value;
        //txtNumpadCurrent.setText(Double.toString(sum));
        
    }
        
    public double getCurrent()
    {
        return dCurrent;
        //return Integer.parseInt(txtNumpadCurrent.getText());
        
    }
    
    public void addToCurrent(double value)
    {
        dCurrent += value;
        //txtNumpadCurrent.setText(txtNumpadCurrent.getText() + value);
    }
    
    /**
     *  add the selected seats to the list
     *  @param moviename
     *  @param count of the selected seats
     *  @param price of the ticket (single)
     */    
    public boolean addItemToList(String item, int count, double price)
    {
        try
        {               
            if (count > 0)
            {                          
                //TODO: current only from last added item
                dCurrent = count*price;
                //txtNumpadCurrent.setText(Double.toString(dCurrent));
                txtNumpadCurrent.setValue(dCurrent);
                txtNumpadCurrentItem.setText(Integer.toString(count) + " x " + item);
                txtNumpadCurrentPrice.setValue(price);
            }
            else
            {
                listModel.clear();
                //txtNumpadCurrent.setText("0");
                txtNumpadCurrent.setValue(0);
                txtNumpadCurrentPrice.setValue(0);
                txtNumpadCurrentItem.setText("0");
                //txtNumpadCurrentPrice.setText("0");
                dCurrent = 0;
            }
                
                
            
            return true;
        }
        catch(Exception e)
        {
            return false;    
        }
        
    }
    
    /**
     *  add the selected seats to the list
     *  @param moviename
     *  @param count of the selected seats
     */    
    public boolean addItemToList(cMovie movie, int count)
    {
        try
        {               
            if (count > 0)
            {                          
                //TODO: current only from last added item
                Movie = new cMovie(); 
                Movie = movie;
                Booking = new cBooking(movie, count);
                dCurrent = count*movie.getPrice();
                //txtNumpadCurrent.setText(Double.toString(dCurrent));
                txtNumpadCurrent.setValue(dCurrent);
                txtNumpadCurrentItem.setText(Integer.toString(count) + " x " + movie.getMovieName());
                txtNumpadCurrentPrice.setValue(movie.getPrice());
            }
            else
            {
                listModel.clear();
                //txtNumpadCurrent.setText("0");
                txtNumpadCurrent.setValue(0);
                txtNumpadCurrentPrice.setValue(0);
                txtNumpadCurrentItem.setText("0");
                //txtNumpadCurrentPrice.setText("0");
                dCurrent = 0;
            }
                
                
            
            return true;
        }
        catch(Exception e)
        {
            return false;    
        }
    } 

    /**
     *  remove all items from the list
     *  
     */        
    public void clearList()
    {
        listModel.clear();
    }
    
    /**
     *  get the list model of the numpadList
     *  @return the 
     */        
    public ListModel getListModel()
    {
        return listModel;
    }
   
    /**
     *  get the change 
     *  @return double of the change
     */     
    private double getCashBack()
    {
        Number num = (Number) txtNumpadCurrentPrice.getValue();
        return getSum() - num.doubleValue();
    }
 
    
}
